package com.sduept.nwld.dataserver.manager.message.interfaces.imp;

import com.sduept.component.message.model.PersonnelConfigure;
import com.sduept.message.entity.AlarmMessage;
import com.sduept.message.entity.AlarmMessageWrapper;
import com.sduept.message.entity.MessageType;
import com.sduept.message.interfaces.MessageSendInterface;
import com.sduept.message.manager.MessageReceiveConfigManager;
import com.sduept.message.manager.MessageTypeManager;
import com.sduept.message.observer.MessageSendObserver;
import com.sduept.nwld.dataserver.exceptions.MessageException;
import com.sduept.nwld.dataserver.manager.message.APPMessagePushManager;
import com.sduept.nwld.dataserver.manager.message.PersonnelMessageConfigureManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @author     Author
 * @version    V1.0
 * @Date       2018年8月23日 下午2:33:36
 */

@Service
public class APPMessageObserver implements MessageSendInterface{
	private static final String MESSAGE_RECEIVER_APP="app";
	
	@Autowired
	private MessageSendObserver messageSendObserver;
	@Autowired
	private MessageReceiveConfigManager messageReceiveConfigManager;
	@Autowired
	private PersonnelMessageConfigureManager configureManager;
	@Autowired
	private APPMessagePushManager appMessagePushManager;
	@Autowired
	private MessageTypeManager messageTypeManager;
	
	@PostConstruct
	public void init(){
		register();//实现类监听注册
	}
	
	@Override
	public void alarmMessageSend(AlarmMessageWrapper alarmMessageWrapper) {
		AlarmMessage alarmMessage = alarmMessageWrapper.getAlarmMessage();//告警消息
		Set<String> givenCodes = new HashSet<String>();//指定人员
		givenCodes.addAll(alarmMessageWrapper.getCodes());
		Set<String> codeSet = new HashSet<String>();//code集合
		//无论是否指定人员，均读取个人消息配置
		List<String> codeConfig = messageReceiveConfigManager.listConfigureIds(alarmMessage.getMsgType(), MESSAGE_RECEIVER_APP);
		if (null != codeConfig && codeConfig.size() >= 0) {
			codeSet.addAll(codeConfig);
		}
		if(null != givenCodes && givenCodes.size() > 0) {//指定人员
			if(alarmMessageWrapper.isForce()){//强制发送
				codeSet.addAll(givenCodes);
			}else{//不强制发送，查取有此接收方式配置的人员，把givenCodes中没配置此消息的人员去除
				Iterator<String> it = givenCodes.iterator();
				while(it.hasNext()){
					String code = it.next();
					if(!codeConfig.contains(code)){//有权限接收此消息的人员列表中无给定人呿
						it.remove();
					}
				}
				codeSet.addAll(givenCodes);
			}
		}
		String[] array = codeSet.toArray(new String[codeSet.size()]);
		Set<String> accounts = findAccountsByCodes(array);//4A账号
		MessageType type = messageTypeManager.findTypeById(alarmMessage.getMsgType());
		String title="";
		if(type!=null){
			title=type.getDescription();
		}
		try {
			List<String> accountsList = new ArrayList<String>();
			accountsList.addAll(accounts);
			appMessagePushManager.messagePush(accountsList, title, alarmMessage.getContent(), alarmMessage.getSource());
		} catch (MessageException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
 	* 根据codes查询人员的4A账号
	 * @param codesArr codes数组
	 * @return 人员手机号
	 */
	private Set<String> findAccountsByCodes(String[] codesArr) {
		Set<String> accountsList = new HashSet<String>();
		List<PersonnelConfigure> personlist = configureManager.getByIds(codesArr);;//根据codes查询人员的4A账号
		for (PersonnelConfigure person : personlist) {
			if (StringUtils.isEmpty(person.getAccount())) {
				continue;
			}
			accountsList.add(person.getAccount());
		}
		return accountsList;
	}

	@Override
	public void register() {
		messageSendObserver.register(this);
	}

}
